package session03;

import com.sun.org.apache.xalan.internal.xsltc.dom.NodeIteratorBase;
import java.io.File;
import java.util.logging.Level;
import javax.xml.crypto.dsig.Transform;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;

public class Main {

    public static void main(String[] args) {
        demo1();
    }

    //get content of title
    private static void demo1(){
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("books.xml"));
            NodeList books  = doc.getElementsByTagName("book");
            System.out.println("Book count : " + books.getLength());
            for (int i = 0; i < books.getLength(); i++) {
                Node book = books.item(i);
                String title = ((Element)book).getElementsByTagName("title").item(0).getTextContent();
                System.out.println(title);
            }

        } catch (Exception e) {
                e.printStackTrace();
        }
    }

    //Buidler to xml
    private static void demo2(){
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("books.xml"));

            Node book = doc.createElement("book");
            Node title = doc.createElement("title");
            title.setTextContent("Toyota the way");

            book.appendChild(title);
            doc.getDocumentElement().appendChild(title);

            TransformerFactory transFac = TransformerFactory.newInstance();
            Transformer transformer = transFac.newTransformer();
            Source source = new DOMSource(doc);
            Result result = new StreamResult(new File("books.xml"));
            transformer.transform(source, result);
            System.out.println("Book saved");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //Get node value
    private static void demo4(){
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File("books.xml"));

            NodeIterator it = ((DocumentTraversal)doc).createNodeIterator(doc, NodeFilter.SHOW_ALL, null, true);
            Node n;
            while((n = it.nextNode()) != null){
                System.out.println(n.getNodeName()+ ":" + n.getNodeValue());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
